<title>商品管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>商品管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-product-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-inline" id="ID-laydate-rangeLinked-product-createdtime">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-start-date-product-list-createdtime" name="createdTimeStart" class="layui-input" placeholder="开始时间">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="ID-laydate-end-date-product-list-createdtime" name="createdTimeEnd" class="layui-input" placeholder="结束时间">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">团长Id</label>
                    <div class="layui-input-block">
                        <script type="text/html" template>
                            <input type="text" name="leaderId" id="ID-input-product-list-leaderId" value="{{= layui.router().search.leaderId }}" placeholder="请输入" autocomplete="off" class="layui-input">
                        </script>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">团长名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="leaderName" id="ID-input-product-list-leaderName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品Id</label>
                    <div class="layui-input-block">
                        <input type="text" name="productId" id="ID-input-product-list-productId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="ID-input-product-list-name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品状态</label>
                    <div class="layui-input-block">
                        <select name="status" id="ID-select-product-list-status">
                            <option value="">不限</option>
                            <option value="1">在架</option>
                            <option value="2">下架</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品类别</label>
                    <div class="layui-input-block">
                        <select name="channelId" id="ID-select-product-list-channelId">
                            <option value="">不限</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">店铺Id</label>
                    <div class="layui-input-block">
                        <input type="text" name="shopId" id="ID-input-product-list-shopId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">店铺名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="shopName" id="ID-input-product-list-shopName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-product-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 查询
                    </button>
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-on="productformlistclear">
                        <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 刷新
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-product-manage" lay-filter="LAY-product-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
                </div>
            </script>
            <script type="text/html" id="table-product-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="handleRemark"><i class="layui-icon layui-icon-edit"></i>处理意见</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="more">
                    更多操作
                    <i class="layui-icon layui-icon-down"></i>
                </a>
            </script>
            <script type="text/html" id="table-useradmin-templet-status">
                {{#  if(d.isEnabled == 'Y'){ }}
                <span style="color: green">启用</span>
                {{#  }else{ }}
                <span style="color: red">禁用</span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-templet-role">
                {{#  if(d.role == 'ADMIN'){ }}
                <span style="">系统管理员</span>
                {{#  }else if(d.role == 'TENANT'){ }}
                <span style="">租户</span>
                {{#  }else{ }}
                <span style="">未知用户</span>
                {{#  } }}
            </script>
            <script type="text/html" id="ID-table-myinvoice-templet-billTypeName">
                <span>{{ d.billTypeName }}</span>
                {{# if(d.billType==1 || d.billType==5){ }}
                <span style="color:#16b777; border: 1px solid; padding: 0px 1px;">普</span>
                {{# }else if(d.billType==2 || d.billType==6){ }}
                <span style="color:#1e9fff; border: 1px solid; padding: 0px 1px;">专</span>
                {{# }else if(d.billType==3){ }}
                <span style="color: #a233c6; border: 1px solid; padding: 0px 1px;">机</span>
                {{# }else if(d.billType==4){ }}
                <span style="color: #ff5722; border: 1px solid; padding: 0px 1px;">铁</span>
                {{#  } }}
            </script>
            <script type="text/html" id="ID-table-myinvoice-templet-invoiceNumber">
                <a class="layui-table-link billtong-myinvoice-table-invoicenumber">{{= d.invoiceNumber }}</a>
            </script>
            <script type="text/html" id="ID-table-product-templet-productId">
                <a class="layui-table-link billtong-myinvoice-table-invoicenumber">{{= d.productId }}</a>
            </script>
            <script type="text/html" id="ID-table-product-templet-status">
                {{# if(d.status==1){ }}
                <span style="color:#16b777; border: 1px solid; padding: 0px 1px;">{{ d.statusName }}</span>
                {{# }else if(d.status==2){ }}
                <span style="color: #ff5722; border: 1px solid; padding: 0px 1px;">{{ d.statusName }}</span>
                {{# }else{ }}
                <span>{{ d.statusName }}</span>
                {{#  } }}
            </script>
            <script type="text/html" id="ID-table-product-templet-handleStatus">
                {{# if(d.handleStatus==1){ }}
                <span style="color: #ff5722; border: 1px solid; padding: 0px 1px;">{{ d.handleStatusName }}</span>
                {{# }else if(d.handleStatus==2){ }}
                <span style="color: #16b777; border: 1px solid; padding: 0px 1px;">{{ d.handleStatusName }}</span>
                {{# }else{ }}
                <span>{{ d.handleStatusName }}</span>
                {{#  } }}
            </script>
        </div>
    </div>
</div>

<script>
    layui.use('product', layui.factory('product')).use(['admin', 'table', 'dropdown', 'util'], function () {
        var $ = layui.$
            , admin = layui.admin
            , setter = layui.setter
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , laydate = layui.laydate
            , util = layui.util
            , dropdown = layui.dropdown;

        function initProductChannel() {
            $.ajax({
                url: setter.baseConfig.apiHost + '/api/product/getallchannels' //实际使用请改成服务端真实接口
                , type: 'post'
                , contentType: "application/json"
                //, data: JSON.stringify(field)
                , beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
                }
                , success: function (res) {
                    if (res.errorCode != '00000') {
                        layer.msg(res.errorMessage, {
                            offset: '15px'
                            , icon: 2
                            , time: 3000
                        });

                        return false;
                    }

                    //初始化商品类别选择框
                    var allChannels = res.data;

                    // 获取选择框元素
                    var select = document.getElementById('ID-select-product-list-channelId');

                    // 动态添加选项
                    allChannels.forEach(function (channel) {
                        var option = document.createElement('option');
                        option.value = channel.channelId;
                        option.textContent = channel.channelName;
                        select.appendChild(option);
                    });

                    //重新渲染选择框
                    form.render('select');
                }
            });
        }

        //初始化商品类别选择框
        initProductChannel();

        form.render(null, 'layadmin-product-formlist');

        //处理自定义事件
        util.on({
            productformlistclear: function () {
                console.log(this); // 当前触发事件的 DOM 元素

                $(':input[name=leaderId]').val('');
                $(':input[name=leaderName]').val('');
                $(':input[name=productId]').val('');
                $(':input[name=name]').val('');
                $('#ID-laydate-start-date-product-list-createdtime').val('');
                $('#ID-laydate-end-date-product-list-createdtime').val('');
                $('#ID-select-product-list-status').val('');
                $('#ID-select-product-list-channelId').val('');
                $('#ID-input-product-list-shopId').val('');
                $('#ID-input-product-list-shopName').val('');
                form.render(null, 'layadmin-product-formlist');

                //执行重载
                table.reload('LAY-product-manage', {
                    where: { }
                });
            }
        });

        // 日期范围 - 左右面板联动选择模式
        //开票日期
        laydate.render({
            elem: '#ID-laydate-rangeLinked-issueDate',
            range: ['#ID-laydate-start-date-myinvoice-list-issuedate', '#ID-laydate-end-date-myinvoice-list-issuedate'],
            rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  2.8+ 新增
        });

        // 日期范围 - 左右面板联动选择模式
        //创建时间
        laydate.render({
            elem: '#ID-laydate-rangeLinked-product-createdtime',
            range: ['#ID-laydate-start-date-product-list-createdtime', '#ID-laydate-end-date-product-list-createdtime'],
            rangeLinked: true,
            type: 'datetime',
            fullPanel: true
        });

        //开启搜索
        form.on('submit(LAY-product-front-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-product-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-product-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-product-manage');
                        layer.msg('已删除');
                    });
                });
            }
            , batchupload: function () {
                admin.popup({
                    title: '发票批量上传'
                    , area: ['500px', '450px']
                    , id: 'LAY-popup-myinvoice-batchupload'
                    , success: function (layero, index) {
                        view(this.id).render('myinvoice/uploadform').done(function () {
                            form.render(null, 'layuiadmin-form-myinvoiceadmin');

                            //开启提交
                            form.on('submit(LAY-user-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                $.ajax({
                                    url: setter.baseConfig.apiHost + '/api/user/add' //实际使用请改成服务端真实接口
                                    , type: 'post'
                                    , contentType: "application/json"
                                    , data: JSON.stringify(field)
                                    , beforeSend: function (xhr) {
                                        xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
                                    }
                                    , success: function (res) {
                                        if (res.errorCode != '00000') {
                                            layer.msg(res.errorMessage, {
                                                offset: '15px'
                                                , icon: 2
                                                , time: 3000
                                            });

                                            return false;
                                        }

                                        //登入成功的提示与跳转
                                        layer.msg('添加成功', {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 3000
                                        });

                                        layui.table.reload('LAY-product-manage'); //重载表格
                                        layer.close(index); //执行关闭
                                    }
                                });
                            });
                        });
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>